package com.frx.leetcode.classify.link;

/**
 * @author ：frx
 * @date ：Created in 2019/11/14 22:40
 * @description：leetcode 找出两个链表的交点
 * @modified By：
 * @version: ToDO
 */
public class IntersectionLink {

    public static void main(String[] args){
        ListNode a3 = new ListNode(3);
        ListNode a2 = new ListNode(2);
        ListNode a1 = new ListNode(1);
        ListNode a0 = new ListNode(0);
        a0.next = a1;
        a1.next = a2;
        a2.next = a3;

        ListNode b3 = new ListNode(3);
        ListNode b2 = new ListNode(2);
        ListNode b1 = new ListNode(4);
        b1.next = b2;
        b2.next = b3;
        System.out.println(getIntersectioNode(a0,b1));
    }

    public static ListNode getIntersectioNode(ListNode headA,ListNode headB){
        ListNode l1 = headA;
        ListNode l2 = headB;
        while (l1!=l2){
            if (l1 == null){
                l1 = headB;
            } else {
                l1 = l1.next;
            }
            if (l2 == null){
                l2 = headA;
            } else {
                l2 = l2.next;
            }
        }
        return l1;
    }


    public static class ListNode{
        int val;
        ListNode next;
        public ListNode(int val){
            this.val = val;
        }

    }
}